package com.mybatis.session;

import com.mybatis.binding.MapperStatement;
import com.mybatis.config.Configuration;
import com.mybatis.config.JdbcProperties;
import org.dom4j.Document;
import org.dom4j.DocumentException;
import org.dom4j.Element;
import org.dom4j.io.SAXReader;

import java.io.IOException;
import java.io.InputStream;
import java.util.List;
import java.util.Properties;

/**
 * @author luoc
 * @version 1.0
 * @date 2021/2/20 10:59
 * @since 1.0
 * 构建sqlSessionFactory,读取配置文件
 */
public class SqlSessionFactoryBuilder {

    public SqlSessionFactory build(InputStream inputStream){

        SAXReader reader = new SAXReader();
        Document document =null;
        Configuration configuration = new Configuration();
        try {
            document = reader.read(inputStream);
            Element configEle = document.getRootElement();

            //读取jdbc的配置文件，然后加载jdbc的配置信息
            Element propertiesEle = configEle.element("properties");
            String jdbcPropertiesPath = propertiesEle.attributeValue("resource");
            loadProperties(configuration,jdbcPropertiesPath);


            //读取mapper.xml 配置文件
            Element mappersEle =  configEle.element("mappers");
            List<Element> mapperList = mappersEle.elements("mapper");
            for (Element e: mapperList) {
                String mapperPath = e.attributeValue("resource");
                loadMapperStatement(configuration,mapperPath);
            }

        } catch (DocumentException e) {
            e.printStackTrace();
        }

        //读取
        return new DefaultSqlSessionFactory(configuration);
    }

    /** 加载配置文件
     * @param configuration
     * @param jdbcPropertiesPath
     * */
    private void loadProperties(Configuration configuration, String jdbcPropertiesPath){
            InputStream inputStream = this.getClass().getClassLoader().getResourceAsStream(jdbcPropertiesPath);
            Properties properties = new Properties();

            try {
                properties.load(inputStream);
            } catch (IOException e) {
                e.printStackTrace();
            }

            JdbcProperties jdbcProperties = new JdbcProperties();
            jdbcProperties.setDriver(properties.getProperty("driver"));
            jdbcProperties.setUrl(properties.getProperty("url"));
            jdbcProperties.setUsername(properties.getProperty("username"));
            jdbcProperties.setPassword(properties.getProperty("password"));

            configuration.setJdbcProperties(jdbcProperties);
    }

     /**
     *  读取mapper.xml配置文件
     * @param configuration 配置
     * @param mapperPath mapper路径
     *
      * */
    private void loadMapperStatement(Configuration configuration,String mapperPath){
        InputStream inputStream = this.getClass().getClassLoader().getResourceAsStream(mapperPath);

        SAXReader reader = new SAXReader();
        Document document =null;

        try {
            document = reader.read(inputStream);
            Element rootEle = document.getRootElement();

            String namespace = rootEle.attributeValue("namespace");
            List<Element> elements = rootEle.elements();
            for (Element ele:elements) {
                MapperStatement mapperStatement = new MapperStatement();
                mapperStatement.setId(namespace +"."+ ele.attributeValue("id"));
                mapperStatement.setQueryType(ele.getName());
                mapperStatement.setResultType(ele.attributeValue("resultType"));
                mapperStatement.setSql(ele.getText());
                configuration.addMapperStatement(mapperStatement);
            }

        } catch (DocumentException e) {
            e.printStackTrace();
        }
    }
}
